﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;

namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblMediaRoomRelatedDAL : ItblMediaRoomRelated
    {
        HKEA_CMSEntities model;
        tblMediaRoom_Related mediaRoomRelated;

        #region 构造函数

        public tblMediaRoomRelatedDAL()
        {
            model = DataAccess.GetModelInstance();
            mediaRoomRelated = model.tblMediaRoom_Related.CreateObject();
        }
        public static tblMediaRoomRelatedDAL GetInstance()
        {
            return new tblMediaRoomRelatedDAL();
        }

        #endregion

        #region 公共函数

        public int InsertList(List<int> MediaUIDList, string strCategory, int ToRelatedUID)
        {
            foreach (var temp in MediaUIDList)
            {
                var tempValue = model.tblMediaRoom_Related.Where(a => a.MediaRoomUID == temp).
                    Where(a => a.RelatedUID == ToRelatedUID).
                    Where(a => a.Category == strCategory).FirstOrDefault();
                if (tempValue == null)
                {
                    tblMediaRoom_Related ent = new tblMediaRoom_Related();
                    ent.RelatedUID = ToRelatedUID;
                    ent.MediaRoomUID = temp;
                    ent.Category = strCategory;
                    ent.IsPublish = 1;
                    ent.Deleted = 0;
                    ent.Created_At = DateTime.Now;
                    ent.Updated_At = DateTime.Now;
                    model.tblMediaRoom_Related.AddObject(ent);
                }
                else
                {
                    if (tempValue.Deleted == 1)
                    {
                        tempValue.Deleted = 0;
                        tempValue.Updated_At = DateTime.Now;
                    }
                }
            }
            return model.SaveChanges();
        }

        public List<M_MediaRoomRelatedDetail> GetList(tblMediaRoom_Related ent, List<int> WithOutList)
        {
            var ret = from related in model.tblMediaRoom_Related
                      where related.Deleted == 0
                      select new M_MediaRoomRelatedDetail()
                      {
                          AppMediaRelatedUID = 0,
                          AppMediaUID = 0,
                          TblMediaUID = related.MediaRoomUID > 0 ? (int)related.MediaRoomUID : 0,
                          AppRelatedUID = 0,
                          TblRelatedUID = related.RelatedUID > 0 ? (int)related.RelatedUID : 0,
                          TblMediaRelatedUID = related.MediaRoomRelatedUID > 0 ? (int)related.MediaRoomRelatedUID : 0,
                          CateGory = related.Category,
                          From = "tbl",
                          IsDeleted = related.Deleted
                      };
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.TblMediaUID == ent.MediaRoomUID);
            if (ent.RelatedUID > 0)
                ret = ret.Where(a => a.TblRelatedUID == ent.RelatedUID);
            if (ent.MediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.TblMediaRelatedUID == ent.MediaRoomRelatedUID);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a => a.CateGory == ent.Category);
            if (WithOutList.Count > 0)
                ret = ret.Where(a => !WithOutList.Contains(a.TblMediaRelatedUID));
            return ret.ToList();
        }

        public int UpdateList(List<int> MediaRelatedUIDList, string strCategory,List<int> MediaUIDList)
        {
            for (int i = 0; i < MediaRelatedUIDList.Count; i++)
            {
                var MediaRelatedUIDValue= MediaRelatedUIDList[i];
                var ent = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID == MediaRelatedUIDValue).Where(a => a.Deleted == 0).FirstOrDefault();
                if (ent != null)
                {
                    ent.Category = strCategory;
                    ent.MediaRoomUID = MediaUIDList[i];
                    ent.Updated_At = DateTime.Now;
                }
            }
            return model.SaveChanges();
        }

        public int DeleteListFalse(List<int> MediaRelatedUIDList)
        {
            foreach (var temp in MediaRelatedUIDList)
            {
                var ent = model.tblMediaRoom_Related.Where(a=>a.MediaRoomRelatedUID==temp).Where(a=>a.Deleted==0).FirstOrDefault();
                if (ent != null)
                {
                    ent.Deleted = 1;
                    ent.Updated_At = DateTime.Now;
                }
            }
            return model.SaveChanges();
        }



        public List<tblMediaRoom_Related> GetListByENT(tblMediaRoom_Related ent)
        {
            var result = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID > 0);
            if (ent.MediaRoomRelatedUID > 0)
                result = result.Where(a=>a.MediaRoomRelatedUID==ent.MediaRoomRelatedUID);
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (ent.RelatedUID > 0)
                result = result.Where(a => a.RelatedUID == ent.RelatedUID);
            return result.OrderByDescending(a => a.MediaRoomRelatedUID).ToList();
        }

        public List<tblMediaRoom_Related> GetListWithOutAppDelByENT(tblMediaRoom_Related ent)
        {
            var result = from related in model.tblMediaRoom_Related
                         where related.RelatedUID==ent.RelatedUID && related.Category == ent.Category && related.Deleted == 0 
                         && !(from tempapprelated in model.app_tblMediaRoom_Related
                              where  tempapprelated.RelatedUID == ent.RelatedUID &&tempapprelated.Category == ent.Category  && tempapprelated.Deleted == 1
                              select tempapprelated.MediaRoomRelatedUID
                         ).Contains(related.MediaRoomRelatedUID)
                         orderby related.MediaRoomRelatedUID descending
                         select related;
            return result.ToList();
        }


        public List<Ext_MediaRoom> GetListWithMediaRoomByENT(tblMediaRoom_Related ent)
        {
            var result = from mrr in model.tblMediaRoom_Related
                         join mr in model.tblMediaRooms on mrr.MediaRoomUID equals mr.MediaRoomUID
                         where mrr.MediaRoomUID > 0 && mr.MediaRoomUID > 0
                         select new Ext_MediaRoom
                         {
                             MediaRoomRelatedUID=mrr.MediaRoomRelatedUID,
                             Category = mrr.Category,
                             RelatedUID=(int)mrr.RelatedUID,
                             Deleted=mrr.Deleted,
                             MediaRoomUID = mr.MediaRoomUID,
                             MediaType = mr.MediaType,
                             FilePath=mr.FilePath,
                             FileName = mr.FileName,
                             FileAlt_Eng = mr.FileAlt_Eng,
                             FileAlt_Chi = mr.FileAlt_Eng,
                             FileLink_Eng = mr.FileLink_Eng,
                             FileLink_Chi = mr.FileLink_Chi,                             
                             Des_Eng = mr.Des_Eng,
                             Des_Chi = mr.Des_Chi,
                             IsPublish = mrr.IsPublish,//关联的字段
                             LastModifyBy = mr.LastModifyBy,
                             CreatedAt = mr.CreatedAt,
                             UpdatedAt = mr.UpdatedAt
                         };
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.RelatedUID > 0)
                result = result.Where(a => a.RelatedUID == ent.RelatedUID);
            result= result.Where(a=>a.Deleted==ent.Deleted);
            return result.OrderByDescending(a => a.CreatedAt).ToList<Ext_MediaRoom>();
        }

        public List<Ext_MediaRoom> GetListWithMediaRoomByENT(Ext_MediaRoom ent)
        {
            var result = from mrr in model.tblMediaRoom_Related
                         join mr in model.tblMediaRooms on mrr.MediaRoomUID equals mr.MediaRoomUID
                         where mrr.MediaRoomUID > 0 && mr.MediaRoomUID > 0
                         select new Ext_MediaRoom
                         {
                             MediaRoomRelatedUID = mrr.MediaRoomRelatedUID,
                             Category = mrr.Category,
                             RelatedUID = (int)mrr.RelatedUID,
                             Deleted = mrr.Deleted,
                             MediaRoomUID = mr.MediaRoomUID,
                             MediaType = mr.MediaType,
                             FilePath = mr.FilePath,
                             FileName = mr.FileName,
                             FileAlt_Eng = mr.FileAlt_Eng,
                             FileAlt_Chi = mr.FileAlt_Eng,
                             FileLink_Eng = mr.FileLink_Eng,
                             FileLink_Chi = mr.FileLink_Chi,
                             Des_Eng = mr.Des_Eng,
                             Des_Chi = mr.Des_Chi,
                             IsPublish = mrr.IsPublish,//关联表的字段
                             Is_Publish = mr.IsPublish,
                             LastModifyBy = mr.LastModifyBy,
                             CreatedAt = mr.CreatedAt,
                             UpdatedAt = mr.UpdatedAt
                         };
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (!string.IsNullOrEmpty(ent.MediaType))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (ent.RelatedUID > 0)
                result = result.Where(a => a.RelatedUID == ent.RelatedUID);
            if (ent.Is_Publish >= 0)
                result = result.Where(a => a.Is_Publish == ent.Is_Publish);
            result = result.Where(a => a.Deleted == ent.Deleted);
            return result.OrderByDescending(a=>a.CreatedAt).ToList<Ext_MediaRoom>();
        }

        public List<tblMediaRoom_Related> GetListByENT(tblMediaRoom_Related ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            var result = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID > 0).Where(a => a.Deleted == ent.Deleted); ;
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip(param.RecordStart).Take(param.PageSize);
            return result.ToList();
        }




        public int GetCountByENT(tblMediaRoom_Related ent)
        {
            var result = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            return result.Count();
        }

        public int InsertENT(tblMediaRoom_Related ent)
        {
            ent.Created_At = DateTime.Now;
            ent.Updated_At = DateTime.Now;
            model.tblMediaRoom_Related.AddObject(ent);
            model.SaveChanges();

            ent = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID > 0).OrderBy(a => a.MediaRoomRelatedUID).ToList().Last();
            return ent.MediaRoomRelatedUID;
        }

        public int UpdateENT(tblMediaRoom_Related ent)
        {
            mediaRoomRelated = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID == ent.MediaRoomRelatedUID).FirstOrDefault();
            if (mediaRoomRelated != null)
            {
                mediaRoomRelated.MediaRoomUID = ent.MediaRoomUID;
                mediaRoomRelated.Category = ent.Category;
                mediaRoomRelated.RelatedUID = ent.RelatedUID;
                mediaRoomRelated.Deleted = ent.Deleted;
                mediaRoomRelated.IsPublish = ent.IsPublish;
                mediaRoomRelated.Updated_At =  DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblMediaRoom_Related ent)
        {
            mediaRoomRelated = model.tblMediaRoom_Related.Where(a => a.MediaRoomRelatedUID == ent.MediaRoomRelatedUID).FirstOrDefault();
            if (mediaRoomRelated != null)
            {
                model.tblMediaRoom_Related.DeleteObject(mediaRoomRelated);
            }
            return model.SaveChanges();
        }

       

        #endregion 
    }
}
